package com.zzmcc.project;

import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.jms.model.Project;
import com.zzmcc.jms.model.ProjectBuild;
import com.zzmcc.jms.model.Tower;
import util.*;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Tower;

public class ProjectBuildController extends BaseController {
    private static final Logger log =  Logger.getLogger(ProjectBuildController.class);

	public void index() {
        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();

        String name = getPara("project.name");
        String projectOwner =getPara("project.projectOwner");

        try {
             name  = URLDecoder.decode(getPara(1), "UTF-8");
            projectOwner  = URLDecoder.decode(getPara(2), "UTF-8");
        } catch (Exception e) {

        }
        if(ChristStringUtil.isNotEmpty(name)){
            whee.append(" and name like ?");
            param.add("%" + name + "%");
        }

        if(ChristStringUtil.isNotEmpty(projectOwner)){
            whee.append(" and projectOwner like ?");
            param.add("%" + projectOwner + "%");
        }

        Page<ProjectBuild> pager = ProjectBuild.dao.paginate(
                getParaToInt(0, 1), 10,
                "select a.id,homeCounty,a.name,a.code,longitude,latitude,addr,createDate,a.modifyDate,a.projectOwner,isCm,isCt,isCu," +
                        "b.isDesign,b.isReach,b.isStart,b.isComplete,b.isSubmit ",
                " from jms_project a left join jms_project_build b on b.pid=a.pid " +
                        " where COALESCE(a.isPlus,'否')='否' and a.isTask='是' and a.isSelectConfirm='是' "+ whee.toString()+"order by modifyDate desc",
                param.toArray());

        setAttr("pager",pager);

        StringBuffer urlParas = new StringBuffer();
        urlParas.append("-");
        if(ChristStringUtil.isNotEmpty(name)){
            urlParas.append(name);
        }
        urlParas.append("-");
        if(ChristStringUtil.isNotEmpty(projectOwner)){
            urlParas.append(projectOwner);
        }
        setAttr("urlParas",urlParas);

        render("index.html");
	}

    public void view(){

        long id=getParaToInt();
        String sql="select a.name,a.pid,a.code," +
                "b.id,b.isReach,isDesign,b.isStart,b.isComplete,b.isSubmit,planCompleteDate,b.modifyMemo,b.memo" +
                " from jms_project a left join jms_project_build b on b.pid=a.pid " +
                " where a.id=?";

        ProjectBuild m= ProjectBuild.dao.findFirst(sql,id);
        setAttr("projectBuild",m);
        render("view.html");
    }

    public void update() {
        ProjectBuild m = getModel(ProjectBuild.class, "projectBuild");

        //更新状态，同时更新完成时间
        String isReach = m.getStr("isReach");
        if (StringUtil.isNotEmpty(isReach) && isReach.equals("是")){
            m.set("reachDate", new Date());
        }
        String isDesign = m.getStr("isDesign");
        if (StringUtil.isNotEmpty(isDesign) && isDesign.equals("是")){
            m.set("designDate", new Date());
        }
        String isStart = m.getStr("isStart");
        if (StringUtil.isNotEmpty(isStart) && isStart.equals("是")){
            m.set("startDate", new Date());
        }
        String isComplete = m.getStr("isComplete");
        if (StringUtil.isNotEmpty(isComplete) && isComplete.equals("是")){
            m.set("completeDate", new Date());
        }
        String isSubmit = m.getStr("isSubmit");
        if (StringUtil.isNotEmpty(isSubmit) && isSubmit.equals("是")){
            m.set("submitDate", new Date());
        }

        ModelUtils.fillForUpdate(m);
        m.update();
        setAttr("operateStr", "进度更新成功!");
        render("manage.html");
    }

    public void save() {
        try {
            ProjectBuild m = getModel(ProjectBuild.class, "projectBuild");

            //更新状态，同时更新完成时间
            String isReach = m.getStr("isReach");
            if (StringUtil.isNotEmpty(isReach) && isReach.equals("是")){
                m.set("reachDate", new Date());
            }
            String isDesign = m.getStr("isDesign");
            if (StringUtil.isNotEmpty(isDesign) && isDesign.equals("是")){
                m.set("designDate", new Date());
            }
            String isStart = m.getStr("isStart");
            if (StringUtil.isNotEmpty(isStart) && isStart.equals("是")){
                m.set("startDate", new Date());
            }
            String isComplete = m.getStr("isComplete");
            if (StringUtil.isNotEmpty(isComplete) && isComplete.equals("是")){
                m.set("completeDate", new Date());
            }
            String isSubmit = m.getStr("isSubmit");
            if (StringUtil.isNotEmpty(isSubmit) && isSubmit.equals("是")){
                m.set("submitDate", new Date());
            }

            ModelUtils.fillForUpdate(m);
            m.save();
            setAttr("operateStr", "进度更新成功!");
        }catch (Exception e){
            setAttr("operateStr", "重复提交，失败！!");

        }
        render("manage.html");
    }


}
